package com.gzz.socket.aio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousSocketChannel;
import java.nio.channels.CompletionHandler;
import java.util.concurrent.ExecutionException;

/**
 *
 */
public class SimpleAIOClient {
    public static void main(String[] args) {
        try {
            AsynchronousSocketChannel client=AsynchronousSocketChannel.open();
            client.connect(new InetSocketAddress("127.0.0.1", 8881), null, new CompletionHandler<Void, Object>() {

                @Override
                public void completed(Void result, Object attachment) {
                    System.out.println("连接服务器成功");
                    ByteBuffer buffer=ByteBuffer.allocate(1024);
                    buffer.putInt(0,1);
                    try {
                        client.write(buffer).get();
                    } catch (InterruptedException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    } catch (ExecutionException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                    buffer.clear();
                    client.read(buffer, buffer, new CompletionHandler<Integer, ByteBuffer>() {

                        @Override
                        public void completed(Integer result, ByteBuffer buffer) {
                            try {
                                //System.out.println("result="+result+" postition="+buffer.position()+" limit="+buffer.limit());
                                buffer.flip();
                                int i=buffer.getInt(0);
                                System.out.println("客户端收到服务器返回的消息"+client.getRemoteAddress().toString()+" "+i);
                                buffer.putInt(0, i+1);
                                Thread.sleep(2000);
                                client.write(buffer).get();
                                buffer.clear();
                                client.read(buffer, buffer, this);
                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            } catch (InterruptedException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            } catch (ExecutionException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }

                        }

                        @Override
                        public void failed(Throwable exc, ByteBuffer attachment) {
                            throw new RuntimeException(exc.getMessage());

                        }
                    });

                }

                @Override
                public void failed(Throwable exc, Object attachment) {
                    throw new RuntimeException(exc.getMessage());
                }
            });
            while(true) {
                Thread.sleep(2000);
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
